package cn.wolfcode.controller;

import cn.wolfcode.annotion.PermissionAnnotation;
import cn.wolfcode.result.JsonResult;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
public class httpClientController {
    @RequestMapping("/getContent")
    @PermissionAnnotation(name="爬虫",expression = "httpClient:getContent")
    @ResponseBody
    public Object getContent(String url) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.声明get请求
        HttpGet httpGet = new HttpGet(url);
        //3.发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4.判断状态码
        String string = null;
        if(response.getStatusLine().getStatusCode()==200){
            HttpEntity entity = response.getEntity();
            //使用工具类EntityUtils，从响应中取出实体表示的内容并转换成字符串
            string = EntityUtils.toString(entity, "utf-8");
        }
        //5.关闭资源
        response.close();
        httpClient.close();
        //6.使用Jsoup解析爬取结果
        Document doc = Jsoup.parse(string);
        Elements ele = doc.getElementsByClass("item_content");
        String text = ele.html();
        return new JsonResult(true,text);
    }
}
